//
//  SINALabelUtils.m
//  sinashebao
//
//  Created by  androidlongs on 2018/2/10.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINALabelUtils.h"

@implementation SINALabelUtils


+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString{
    return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:16];
}

+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTextSize:(NSInteger)fontSize{
    return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:fontSize];
}

+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize{
    
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.0) {
//
//
//        CGSize size = [tagString sizeWithFont:tagLabel.font constrainedToSize:CGSizeMake(tagWidth, tagHeight)];
//        // 计算出文本高度后,重新给label赋值
//        CGRect frame = tagLabel.frame;
//        frame.size.height = size.height;
//        frame.size.width = size.width;
//        tagLabel.frame = frame;
//    } else {
        /*
         // 计算text的高度,改变label的高,让label适应这些文字
         CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:18.0] constrainedToSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
         // 第一个参数:字体
         // 第二个参数:constraintedSize
         // 第三个参数:换行模式
         */
        // after iOS7: boundingRectWithSize:options:attributes:context:
        CGSize textSize = [tagString boundingRectWithSize:CGSizeMake(tagWidth, tagHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size;
        // 选项options:
        /**
         * NSStringDrawingUsesLineFragmentOrigin:
         绘制文本时使用 line fragement origin 而不是 baseline origin
         NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
         * NSStringDrawingUsesFontLeading:
         计算行高时使用行距。(译者注:字体大小+行间距=行距)
         * NSStringDrawingTruncatesLastVisibleLine:
         如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
         * NSStringDrawingUsesDeviceMetrics:
         计算布局时使用图元字形(而不是印刷字体)。
         */
        CGRect frame = tagLabel.frame;
        frame.size.height = textSize.height;
        frame.size.width = textSize.width;
        tagLabel.frame = frame;
//    }
    tagLabel.numberOfLines=0;
    tagLabel.text=tagString;
    
    return tagLabel;
}
+(CGSize )measureLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize{
    
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.0) {
//        CGSize size = [tagString sizeWithFont:tagLabel.font constrainedToSize:CGSizeMake(tagWidth, tagHeight)];
//        return size;
//    } else {
        /*
         // 计算text的高度,改变label的高,让label适应这些文字
         CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:18.0] constrainedToSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
         // 第一个参数:字体
         // 第二个参数:constraintedSize
         // 第三个参数:换行模式
         */
        // after iOS7: boundingRectWithSize:options:attributes:context:
        CGSize textSize = [tagString boundingRectWithSize:CGSizeMake(tagWidth, tagHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size;
        // 选项options:
        /**
         * NSStringDrawingUsesLineFragmentOrigin:
         绘制文本时使用 line fragement origin 而不是 baseline origin
         NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
         * NSStringDrawingUsesFontLeading:
         计算行高时使用行距。(译者注:字体大小+行间距=行距)
         * NSStringDrawingTruncatesLastVisibleLine:
         如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
         * NSStringDrawingUsesDeviceMetrics:
         计算布局时使用图元字形(而不是印刷字体)。
         */
       
        return textSize;
//    }
    
}


@end
